use std::char::from_digit;

pub fn convert_base(num_str: &str, to_base: u32) -> String {
    // TODO: 这里写逻辑
    let idx = num_str.find('(').unwrap();

    let (from_num, from_base) = num_str.split_at(idx);
    let from_base = &from_base[1..from_base.len() - 1];
    let from_base: u32 = from_base.parse().unwrap();

    let mut num = 0;
    let mut sign = 1;
    for x in from_num.chars() {
        if x == '-' {
            sign *= -1;
        }
        else if x == '+' {
            sign *= 1;
        }
        else {
            num = num * from_base + x.to_digit(from_base).unwrap();
        }
    }
    let mut to_num = String::new();
    while num > 0 {
        to_num = from_digit(num % to_base, to_base).unwrap().to_string() + to_num.as_str();
        num /= to_base;
    }
    if sign == -1 {
        to_num = "-".to_string() + to_num.as_str();
    }
    to_num

}
